package com.example;


import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toSet;

/**
 * @Description:
 * @Author: 赵超
 * @Copyright: Copyright (c) 浙江日报
 * @Date: 2023-05-05 19:04
 * <p>
 * https://openjdk.org/jeps/269
 * @Version: 1.0
 */

public class JEP269 {

    @Test
    public void testList() {

        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        //JDK8
        List<Integer> list2 = Arrays.asList(1, 2, 3, 4);
        //
        List<Integer> list3 = Collections.unmodifiableList(list1);

        List<Integer> list4 = Collections.unmodifiableList(new ArrayList<Integer>() {{
            add(1); add(2); add(3);
        }});

        List<Integer> list5 = Collections.unmodifiableList(Stream.of(1,2,3,4).collect(Collectors.toList()));
        //guava
        List<Integer> list6 = com.google.common.collect.Lists.newArrayList(1, 2, 3);

        List<Integer> list7 = com.google.common.collect.ImmutableList.of(1, 2, 3, 4);
        //hutool
        List<Integer> list8 = cn.hutool.core.collection.CollUtil.newArrayList(1, 2, 3, 4, 5, 6);

        //JDK17
        List<Integer> newList = List.of(1, 2);
    }

    @Test
    public void testSet() {

        Set<String> set = new HashSet<>();
        set.add("a");
        set.add("b");
        set.add("c");





        Set<String> set2 = Collections.unmodifiableSet(set);
        Set<String> set3 = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));
        Set<String> set4 = Collections.unmodifiableSet(new HashSet<String>() {{
            add("a"); add("b"); add("c");
        }});

        //guava不可变SET
        Set<String> set5 = com.google.common.collect.ImmutableSet.of("a","b","c");
        //guava可变SET
        Set<String> set6 = com.google.common.collect.Sets.newHashSet("a","b","c");

        //hutool
        Set<String> list7 = cn.hutool.core.collection.CollUtil.newHashSet("a","b","c");

        //he Java 8 Stream API can be used to construct small collections, by combining stream factory methods and collectors. For example,
        Set<String> set8 = Collections.unmodifiableSet(Stream.of("a", "b", "c").collect(toSet()));
        Set<String> set9 = Set.of("a", "b", "c");
    }

    @Test
    public void testMap() {

        Map<String, Integer> map1=new HashMap<>();
        map1.put("a",1);
        map1.put("b",2);
        map1.put("c",3);


        Map<String, Integer> map2 = Collections.unmodifiableMap(map1);
        Map<String, Integer> map3 = Collections.unmodifiableMap(new HashMap <String,Integer>() {{
            put("a",1); put("b",2); put("c",3);
        }});

        Map<String, Integer> map4= ImmutableMap.of("a",1,"b",2,"c",3);


        Map<String, Integer> map5 = Map.of("a", 1, "b", 2, "c", 3);
        Map<String, Integer> map6 = Map.ofEntries(
                Map.entry("a", 1),
                Map.entry("b", 2),
                Map.entry("c", 3)
        );

    }


}
